//
//  MTNewClubRemarkCell.swift
//  MT
//
//  Created by ooops on 2020/5/10.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import Foundation
import Eureka
import MTCategory
import GrowingTextView
import RxSwift
import RxCocoa


public class MTNewClubRemarkCell: Cell<String>, CellType {
    
    /// 显示模式
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "名称"
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    /// 多行文本编辑
    lazy var clubRemarkView: GrowingTextView = {
        let textView = GrowingTextView()
        textView.frame = CGRect(x: 54, y: 190, width: 300, height: 100)
        textView.layer.cornerRadius = 3.0
        textView.font = PingFangSCMediumFont(14)
        textView.textColor = UIColor.text.x9b9b9b
        textView.maxLength = 50
        //textView.delegate = self
        textView.minHeight = 100
        textView.maxHeight = 100
        //textView.keyboardAppearance = .dark
        //textView.isHidden = true
        return textView
    }()
    
    lazy var contentLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(14)
        l.textColor = UIColor.text.x9b9b9b
        //l.text = "上海市长宁区北翟路110号长宁区北翟路110号"
        l.numberOfLines = 0
        l.sizeToFit()
//        l.lineBreakMode = .byCharWrapping
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
//        v.isHidden = true
        return v
    }()
    
    public var clubRemarkViewHeight: CGFloat = 50 {
        didSet {
            clubRemarkView.minHeight = clubRemarkViewHeight
        }
    }
    
    public var clubRemarkViewScrollEnable: Bool = true {
        didSet {
            clubRemarkView.isScrollEnabled = clubRemarkViewScrollEnable
        }
    }
    
    public var tipString: String? = nil
    public var contentString: String? {
        didSet {
            if let c = contentString, c.count > 1 {
                clubRemarkView.isEditable = false
                clubRemarkView.backgroundColor = UIColor(hex: 0xF4F4F4)
                clubRemarkView.font = PingFangSCMediumFont(12)
                clubRemarkView.textColor = UIColor.text.x9b9b9b
                clubRemarkView.text = c
                tipLabel.backgroundColor = UIColor(hex: 0xF4F4F4)
                lineView.backgroundColor = UIColor(hex: 0xe6e6e6)
                
                tipLabel.snp.updateConstraints { (make) in
                    //make.centerY.equalToSuperview()
                    make.top.equalToSuperview().offset(19)
                    make.left.equalToSuperview().offset(20)
                }
                
                clubRemarkView.snp.updateConstraints { (make) in
                    make.bottom.equalToSuperview().offset(-19)
                }
                
                lineView.snp.updateConstraints { (make) in
                    make.left.equalToSuperview().offset(10)
                    make.right.equalToSuperview().offset(-10)
                }
                
                contentView.backgroundColor = UIColor(hex: 0xF4F4F4)
                
            } else {
                clubRemarkView.isEditable = true
                clubRemarkView.backgroundColor = UIColor(hex: 0xffffff)
                tipLabel.backgroundColor = UIColor(hex: 0xffffff)
                clubRemarkView.font = PingFangSCMediumFont(14)
                clubRemarkView.textColor = UIColor.text.x9b9b9b
                lineView.backgroundColor = UIColor.seprateLine.x707070
                
                tipLabel.snp.updateConstraints { (make) in
                    make.top.equalToSuperview().offset(10)
                    make.left.equalToSuperview().offset(10)
                }
                
                clubRemarkView.snp.updateConstraints { (make) in
                    make.bottom.equalToSuperview().offset(-10)
                }
                
                lineView.snp.updateConstraints { (make) in
                    make.left.equalToSuperview().offset(0)
                    make.right.equalToSuperview().offset(0)
                }
                
                contentView.backgroundColor = UIColor(hex: 0xffffff)
            }
            if let c = contentString,c == " " {
                lineView.backgroundColor = UIColor.seprateLine.x707070
                lineView.snp.updateConstraints { (make) in
                    make.height.equalTo(0.5)
                }
            }
        }
    }
    /// 输入框提示文字
    public var placeholder: String? {
        didSet {
            if placeholder != nil {
                let attributeRemark: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : PingFangSCRegularFont(14),
                NSAttributedString.Key.foregroundColor : UIColor(hex: 0x5d5d5d)];
                
                clubRemarkView.attributedPlaceholder = NSAttributedString(string: placeholder!, attributes: attributeRemark)
            }
        }
    }
    
    /**
     * 0 - 左右结构 xxx - xxxxxx
     * 1 - 左右结构 xxxx
     *             xxxxxxx
     */
    
    public var contentType: MTClubDetailTextCellContentEditType = .editTextSingle {
        didSet {
            //textValue()
        }
    }
    
    /** 是否编辑模式
     * 是否显示多行文本编辑 取决于 contentType == 1
     */
    public var isEditMode: Bool = true {
        didSet {
            clubRemarkView.isUserInteractionEnabled = isEditMode
        }
    }
    
    public var maxTextLength: Int = 0 {
        didSet {
            if isEditMode && contentType == .editTextMutiply {
                clubRemarkView.maxLength = maxTextLength
            }
        }
    }
    
    private var isHiddenLineView: Bool = true {
        didSet {
            lineView.isHidden = isHiddenLineView
        }
    }
    
    open override func cellCanBecomeFirstResponder() -> Bool {
        if isEditMode {
            return clubRemarkView.canBecomeFirstResponder
        }
        return false
    }

    open override func cellBecomeFirstResponder(withDirection direction: Direction) -> Bool {
        if isEditMode {
            return clubRemarkView.becomeFirstResponder()
        }
        return false
    }

    open override func cellResignFirstResponder() -> Bool {
        //return (contentType == .editTextSingle ? contentTextField.resignFirstResponder() : clubRemarkView.resignFirstResponder())
        if isEditMode {
            return clubRemarkView.resignFirstResponder()
        }
        return false
    }
    
    
    override public func setup() {
        super.setup()
        self.backgroundColor = UIColor.theme.background
        self.selectionStyle = .none
        self.textLabel?.isHidden = true
        self.detailTextLabel?.isHidden = true
        self.height = { UITableView.automaticDimension }
        setupUI()
        makeConstraint()
        textValue()
        if row.value != nil {
            clubRemarkView.text = row.displayValueFor?(row.value)
        }
    }
    
    
    override public func update() {
        super.update()
        tipLabel.text = tipString
        if row.value != nil {
            clubRemarkView.text = row.displayValueFor?(row.value)
        }
    }
}

// MARK: - 布局相关
extension MTNewClubRemarkCell {
    
    private func setupUI() {
        contentView.addSubview(tipLabel)
//        contentView.addSubview(contentLabel)
        //contentView.addSubview(contentTextField)
        contentView.addSubview(lineView)
        contentView.addSubview(clubRemarkView)
//        contentView.addSubview(customAccessoryView)
    }
    
    private func makeConstraint() {
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(0)
            make.height.equalTo(1)
            make.bottom.equalToSuperview()
        }
        
        tipLabel.snp.makeConstraints { (make) in
            //make.centerY.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(10)
            make.width.greaterThanOrEqualTo(30).priority(.high)
        }
        
        clubRemarkView.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(8)
            make.right.equalToSuperview().offset(-6)
            make.left.equalTo(tipLabel).offset(-4)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
}

// MARK: - 文本编辑相关
extension MTNewClubRemarkCell {
    
    private func textValue() {
        
        clubRemarkView.rx.didChange.subscribe(onNext: { [weak self] text in
            guard let weakSelf  = self else { return }
            let value: String = weakSelf.clubRemarkView.text
                        
            if value.count == 0 {
                weakSelf.clubRemarkView.text = ""
            } else {
                if weakSelf.maxTextLength != 0 && value.count > weakSelf.maxTextLength {
                    weakSelf.clubRemarkView.text = value.mySubString(to: weakSelf.maxTextLength)
                }
                weakSelf.row.value = weakSelf.clubRemarkView.text
            }
            
            weakSelf.row.value = weakSelf.clubRemarkView.text
            /// 更新 cell
//            weakSelf.row.updateCell()
        }).disposed(by: globalDisposedBag)
    }
    
}


// The custom Row also has the cell: CustomCell and its correspond value
public final class MTNewClubRemarkCellRow: Row<MTNewClubRemarkCell>, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
        cellProvider = CellProvider<MTNewClubRemarkCell>.init()
    }
}
